package com.glory.wxelectricitypda.activity;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.ItemMTConsumeAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.ConsumeListEntity;
import com.glory.wxelectricitypda.bean.LotBean;
import com.glory.wxelectricitypda.bean.MTConsumeEntity;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;

public class NewAbortActivity extends BaseDialogActivity{


    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.tvStep)
    TextView tvStep;
    @BindView(R.id.tvLotQty)
    TextView tvLotQty;
    @BindView(R.id.tvAttribute1)
    TextView tvAttribute1;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.rvBomList)
    RecyclerView rvBomList;
    @BindView(R.id.tvPartName)
    TextView tvPartName;

    private ItemMTConsumeAdapter mItemMTConsumeAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_abort_new;
    }

    @Override
    protected void initView() {
        freeUI_titleView.setTitle("退站");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (etLotId.getText().toString().equals("")){
                    showToast("请扫描批次编号",false);
                    return;
                }
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("LotID", etLotId.getText().toString());
                    jsonObject.put("Comments", etRemark.getText().toString());
                    ArrayList<MTConsumeEntity> mtConsumeEntities = new ArrayList<>();
                    for (ConsumeListEntity.ConsumeListDTO datum : mItemMTConsumeAdapter.getData()) {
                        MTConsumeEntity mtConsumeEntity = new MTConsumeEntity();
                        mtConsumeEntity.setConsume(Integer.parseInt(datum.getConsume().equals("")?"0":datum.getConsume()));
                        mtConsumeEntity.setMLotId(datum.getMlotid());
                        mtConsumeEntity.setMType(datum.getMaterialtype());
                        mtConsumeEntity.setMTType(datum.getMttype());
                        mtConsumeEntity.setPosition(datum.getPosition());
                        mtConsumeEntity.setMCode(datum.getMaterialname());
                        mtConsumeEntities.add(mtConsumeEntity);
                    }
                    jsonObject.put("XLotList", new JSONArray(new Gson().toJson(mtConsumeEntities)));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                NetUtils.requestPostUtils("abort","Abort",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<Object>>(
                        new TypeToken<ResponseBean<Object>>() {
                        }.getType(), getSupportFragmentManager(), this) {
                    @Override
                    public void onSuccess(Response<ResponseBean<Object>> response) {
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            showToast("退站成功",true);
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    finish();
                                }
                            },2000);
                        }
                    }

                    @Override
                    public void onError(Response<ResponseBean<Object>> response) {
                        super.onError(response);
                        loadError(response.getException(), "Abort");
                    }
                });
            }
        });
        rvBomList.setLayoutManager(new LinearLayoutManager(this));
        rvBomList.setItemAnimator(new DefaultItemAnimator());
        rvBomList.addItemDecoration(new DividerItemDecoration(this, 1));
        mItemMTConsumeAdapter = new ItemMTConsumeAdapter(null);
        mItemMTConsumeAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mItemMTConsumeAdapter.isFirstOnly(true);
        rvBomList.setAdapter(mItemMTConsumeAdapter);
    }

    @Override
    protected void initData() {
        etLotId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(final CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    mItemMTConsumeAdapter.setNewData(null);
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("LotID", s.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    NetUtils.requestPostUtils("getLotInfo","GetLotInfo",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<LotBean>>(
                            new TypeToken<ResponseBean<LotBean>>() {
                            }.getType(), getSupportFragmentManager(), this) {
                        @Override
                        public void onSuccess(Response<ResponseBean<LotBean>> response) {
                            if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                                tvStep.setText(response.body().getBody().getLotInfo().getSTEPDESC());
                                tvLotQty.setText(response.body().getBody().getLotInfo().getMAINQTY());
                                tvAttribute1.setText(response.body().getBody().getLotInfo().getATTRIBUTE1());
                                tvPartName.setText(response.body().getBody().getLotInfo().getPARTNAME());
                                getEqpAllConsume(s.toString());
                            }
                        }

                        @Override
                        public void onError(Response<ResponseBean<LotBean>> response) {
                            super.onError(response);
                            loadError(response.getException(), "GetLotInfo");
                        }
                    });
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }


    void getEqpAllConsume(String lotID){
        JSONObject map = new JSONObject();
        try {
            map.put("LotID", lotID);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("getEqpAllConsume","GetEqpAllConsume",map,this, new FreeUI_DialogEntityCallBack<ResponseBean<ConsumeListEntity>>(
                new TypeToken<ResponseBean<ConsumeListEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<ConsumeListEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    mItemMTConsumeAdapter.setNewData(response.body().getBody().getConsumeList());
                }
            }

            @Override
            public void onError(Response<ResponseBean<ConsumeListEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "GetEqpAllConsume");
            }
        });
    }

}
